<template lang="pug">
  Card.siteSetting
    header.siteSetting-header
      Button(v-if="tabsName==='accountSetting'",type='primary',@click='$refs["accountSetting"].acountModel=true') 新建
      Button(v-else,type='primary',@click='handleSave') 保存
      Button(@click="$router.push('/mould/site')") 返回
    section.siteSetting-body
      Tabs(v-model='tabsName')
        TabPane(label='登录页面设置',name='loginSetting')
          loginSet(ref='loginSetting')
        TabPane(v-if="$route.query.name==='1'",label='采集设置',name='captureSetting')
          captureSet(ref="captureSetting")
        TabPane(v-if="$route.query.name==='2'",label='账号设置',name='accountSetting')
          accountSet(ref="accountSetting")
</template>

<script>
import { asyncSite } from '../api'
import loginSet from './components/login-setting'
import captureSet from './components/capture-setting'
import accountSet from './components/account-setting'

export default {
  components: { loginSet, captureSet, accountSet },
  data() {
    return {
      tabsName: 'loginSetting'
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$store.dispatch('breadcrumb/getBreadCrumb', [{ name: '模板中心' }, { name: '站点管理' }, { name: this.$route.query.siteName }])
    })
  },
  methods: {
    handleSaveValidate() {
      const url = new RegExp(
        '^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$',
        'i'
      )
      if (
        (this.$refs['loginSetting'].formValidate.authCodeType === 0 || this.$refs['loginSetting'].formValidate.authCodeType === 3) &&
        this.$refs['loginSetting'].formValidate.authCodeRule === '' &&
        this.$refs['loginSetting'].formValidate.getAuthCodeUrl === ''
      ) {
        this.$Message.warning('验证码位置规则与获取验证码url至少填写一个')
        return false
      }
      // if (this.$refs['loginSetting'].formValidate.submitButtonRule === '') {
      //   this.$Message.warning('登录按钮规则不能为空')
      //   return false
      // }
      // if (this.$refs['loginSetting'].formValidate.loginSuccessJudgeRule === '') {
      //   this.$Message.warning('登录成功判定规则不能为空')
      //   return false
      // }
      if (this.$route.query.name === '1') {
        if (this.$refs['captureSetting'].portPerson.downloadType === 1) {
          if (this.$refs['captureSetting'].portPerson.requestUrl === '') {
            this.$Message.warning('个人信息请求Url不能为空')
            this.tabsName = 'captureSetting'
            return false
          }
          // if (!url.test(this.$refs['captureSetting'].portPerson.requestUrl)) {
          //   this.$Message.warning('个人信息请求Url格式错误，非正确Url格式')
          //   this.tabsName = 'captureSetting'
          //   return false
          // }
        }
        if (this.$refs['captureSetting'].portPerson.downloadType === 2) {
          if (this.$refs['captureSetting'].portPerson.clickElementXpathRules === '') {
            this.$Message.warning('个人信息Xpath规则不能为空')
            this.tabsName = 'captureSetting'
            return false
          }
        }
        for (let i = 0, len = this.$refs['captureSetting'].pageItems.length; i < len; i++) {
          if (this.$refs['captureSetting'].pageItems[0].downloadType === 1) {
            if (this.$refs['captureSetting'].pageItems[i].requestUrl === '') {
              this.$Message.warning(`缴费信息page${i + 1}请求Url不能为空`)
              this.tabsName = 'captureSetting'
              return false
            }
            // if (!url.test(this.$refs['captureSetting'].pageItems[i].requestUrl)) {
            //   this.$Message.warning(`缴费信息page${i + 1}请求Url格式错误，非正确Url格式`)
            //   this.tabsName = 'captureSetting'
            //   return false
            // }
          }
          if (this.$refs['captureSetting'].pageItems[0].downloadType === 2 && this.$refs['captureSetting'].pageItems[i].clickElementXpathRules === '') {
            this.$Message.warning(`缴费信息page${i + 1}Xpath规则不能为空`)
            this.tabsName = 'captureSetting'
            return false
          }
        }
      }
      return true
    },
    handleSave() {
      this.$refs['loginSetting'].$refs['loginSet'].validate(async valid => {
        if (valid) {
          if (this.handleSaveValidate()) {
            try {
              const socialSecurityRulesData = () => {
                if (this.$route.query.name === '1') {
                  return {
                    socSecLogin: JSON.stringify(this.$refs['loginSetting'].formValidate),
                    siteSocialSecurityRules: JSON.stringify([this.$refs['captureSetting'].portPerson, ...this.$refs['captureSetting'].pageItems])
                  }
                } else {
                  return { socSecLogin: JSON.stringify(this.$refs['loginSetting'].formValidate) }
                }
              }
              await asyncSite.addSocialSecurity({ data: socialSecurityRulesData() })
              this.$Message.success('保存成功')
              this.$router.push('/mould/site')
            } catch (error) {}
          }
        } else {
          this.$Message.warning('表单信息未填写完全，请滑动页面查案未填写表单')
          this.tabsName = 'loginSetting'
        }
      })
    }
  }
}
</script>

<style lang="scss">
@import './index.scss';
</style>

